const express = require('express')
const app = express()
bodyParser = require('body-parser')
// 业务文件
const users = require('./routes/api/users.js')
const profiles = require('./routes/api/profiles.js')
// 数据库
const keys = require('./config/keys.js')
const db = keys.mongodbUrl
const mongoose = require('mongoose')
// 配置bodyparser
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

// 配置passport用于验证token
const passport = require('passport')
app.use(passport.initialize());
require('./config/passport.js')(passport)


mongoose.connect(db,{ useNewUrlParser: true })
.then(() => {
  console.log('mongodb is connected')
})
.catch((err) => {
  console.log(err)
})



app.use('/api/users',users)


app.use('/api/profiles',profiles)


const port = process.env.PORT || 5001
app.listen(port, () => {
  console.log('servere is running...' + port)
})